package com.wangzhenyu.learn.test.android_.fragmentViewModel

import android.os.Bundle
import android.view.*
import androidx.core.view.MenuProvider
import com.wangzhenyu.learn.R
import com.wangzhenyu.learn.databinding.TestFragmentFirstBinding
import com.wangzhenyu.common.base.BaseVBFragment
import com.wangzhenyu.common.fragment.DatePickerFragment

import com.wangzhenyu.learn.test.android_.intent_.InstanceOfIntent
import java.text.SimpleDateFormat
import java.util.*

class FirstFragment : BaseVBFragment<TestFragmentFirstBinding>(), MenuProvider {

    override fun getBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ) = TestFragmentFirstBinding.inflate(inflater, container, false)

    override fun onCreateContextMenu(
        menu: ContextMenu,
        v: View,
        menuInfo: ContextMenu.ContextMenuInfo?
    ) {
        super.onCreateContextMenu(menu, v, menuInfo)
        layoutInflater
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        //菜单
        activity?.addMenuProvider(this)

        datePickerFragment()

        binding?.apply {

            btTest01.setOnClickListener {
                DatePickerFragment.newInstance(GregorianCalendar(1997, 0, 27).time).apply {
                    show(this@FirstFragment.parentFragmentManager, DatePickerFragment.TAG)
                }
            }

            btTest02.setOnClickListener {
                activity?.let { it1 -> InstanceOfIntent.test01(it1) }
            }
        }

    }

    override fun onDestroyView() {
        super.onDestroyView()
        activity?.removeMenuProvider(this)
    }

    private fun datePickerFragment() {
        //注册对DatePickerFragment的监听
        parentFragmentManager.setFragmentResultListener(
            DatePickerFragment.KEY_DATE_PICKER, viewLifecycleOwner
        ) { requestKey, result ->
            if (requestKey == DatePickerFragment.KEY_DATE_PICKER) {
                val date = result.getSerializable(DatePickerFragment.ARG_DATE_PICKER_RESULT) as Date
                val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
                binding?.btTest01?.text = dateFormat.format(date)
            }
        }
    }

    override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
        menuInflater.inflate(R.menu.test_menu, menu)
    }

    override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
        return when (menuItem.itemId) {
            R.id.test_menu_one -> {
                //doSomething
                true
            }
            else -> {

                true
            }
        }
    }


}